package cn.edu.nchu.fragmentdemo.customcomponents;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import cn.edu.nchu.fragmentdemo.R;

/**
 * @className: CustomSeekbarWithText
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2023/09/21 11:11
 **/
public class UavCustomSeekbar extends LinearLayout {

    private SeekBar seekBar;
    private TextView titleTextView;

    public SeekBar getSeekBar() {
        return seekBar;
    }

    private TextView processTextView;
    @RequiresApi(api = Build.VERSION_CODES.O)
    public UavCustomSeekbar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Seekbar);
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.customcomponent_uav_seekbar, this);
        seekBar = findViewById(R.id.seekbar_seekBar);
        titleTextView = findViewById(R.id.seekbar_title_textview);
        processTextView = findViewById(R.id.seekbar_progress_textview);

        titleTextView.setText(ta.getText(R.styleable.Seekbar_seekbar_title));
        seekBar.setMin(ta.getInteger(R.styleable.Seekbar_seekbar_min, 0));
        seekBar.setMax(ta.getInteger(R.styleable.Seekbar_seekbar_max, 100));
        final int multiple = ta.getInt(R.styleable.Seekbar_seekbar_multiple, 1);
        seekBar.setProgress(ta.getInteger(R.styleable.Seekbar_seekbar_progress, 0));

        int progress = seekBar.getProgress();
        String formattedProgress;
        if (progress == 0) {
            formattedProgress = "0.0";
        } else {
            formattedProgress = String.valueOf(seekBar.getProgress() / 1.0f / multiple);
        }
        processTextView.setText(formattedProgress);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 当进度改变时，更新TextView的文本
                float decimalProgress = progress / 1.0f / multiple;
                processTextView.setText(String.valueOf(decimalProgress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 开始拖动SeekBar时的回调
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // 停止拖动SeekBar时的回调
            }
        });
        ta.recycle();

    }

}
